package com.pinjitsu.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.pinjitsu.entity.Highscore;
import com.pinjitsu.entity.Highscores;

@Controller
public class HighscoreController {

    @RequestMapping(value = "/highscore/submit", method = RequestMethod.POST)
    public ResponseEntity<String> saveHighscore(
            @RequestParam(value = "h") Long score,
            @RequestParam(value = "i") String initial,
            HttpServletRequest request) {

        HttpHeaders responseHeaders = new HttpHeaders();
        if (addHighscore(initial, score) != null) {
            return new ResponseEntity<String>("", responseHeaders,
                    HttpStatus.OK);
        }
        return new ResponseEntity<String>("", responseHeaders,
                HttpStatus.BAD_REQUEST);

    }

    private Key addHighscore(String initial, Long score) {
        DatastoreService datastore = DatastoreServiceFactory
                .getDatastoreService();
        Entity highscoreEntity = new Entity("Highscore");
        highscoreEntity.setProperty("score", score);
        highscoreEntity.setProperty("initial", initial);
        highscoreEntity.setProperty("timestamp", new Date());

        return datastore.put(highscoreEntity);
    }

//    @RequestMapping(value = "/highscore/list2")
//    @ResponseBody
//    public Highscores test2(HttpServletRequest request) {
//
////        List<Highscore> highscores = new ArrayList<Highscore>();
//        Highscores highscores = new Highscores();
//
//
//        Highscore highscore = new Highscore();
//        highscore.setScore(12345L);
//        highscore.setInitial("EDD");
//        highscore.setTimestamp(new Date());
//        highscore.setId(1L);
//        highscores.addHighscore(highscore);
//        highscores.addHighscore(highscore);
//
//        return highscores;
//    }

    @RequestMapping(value = "/highscore/list")
    @ResponseBody
    public Highscores listHighscore(HttpServletRequest request) {
        DatastoreService datastore = DatastoreServiceFactory
                .getDatastoreService();
        Query q = new Query("Highscore");
        q.addSort("score", SortDirection.DESCENDING);
        List<Entity> result = datastore.prepare(q).asList(
                FetchOptions.Builder.withLimit(10));
        Highscores highscores = new Highscores();
        for (Entity e : result) {
            Highscore highscore = new Highscore();
            highscore.setScore((Long) e.getProperty("score"));
            highscore.setInitial((String) e.getProperty("initial"));
            highscore.setTimestamp((Date) e.getProperty("timestamp"));
            highscore.setId(e.getKey().getId());
            highscores.addHighscore(highscore);
        }
        return highscores;
    }

    private Highscore getHighscoreFromDatastore(String initial) {
        DatastoreService datastore = DatastoreServiceFactory
                .getDatastoreService();
        Query q = new Query("Highscore");
        q.addFilter("initial", FilterOperator.EQUAL, initial);
        q.addSort("score", SortDirection.DESCENDING);
        List<Entity> result = datastore.prepare(q).asList(
                FetchOptions.Builder.withLimit(1));
        Highscore highscore = new Highscore();
        if (!result.isEmpty()) {
            Entity e = (Entity) result.get(0);
            highscore.setScore((Long) e.getProperty("score"));
            highscore.setInitial((String) e.getProperty("initial"));
            highscore.setTimestamp((Date) e.getProperty("timestamp"));
            highscore.setId(e.getKey().getId());
        }
        return highscore;
    }

}
